# encoding: utf-8
import functools
from user import User
from sdktest_module.utils.constant import *


# def user_num(num):
#     def decorator(func):
#
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
#
#             return func(*args, **kwargs)
#
#         wrapper.user_num = num
#         return wrapper
#
#     return decorator

def user_num(num):
    """
    用于class的装饰器
    """
    def decorator(cls):
        cls.user_num = num
        return cls
    return decorator


def case(id, name, user_num=2, creator='测试开发', os=[ANDROID]):
    """
    用例信息装饰器
    id:用例ID
    name:用例中文名
    creator:用例创建人
    """
    def decorator(func):

        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            ret = func(self, *args, **kwargs)

            # 检查回调信息是否为空
            # 遍历userA, userB, userC.., 反射调用checkCallbackEmpty
            user_list = [getattr(self, attr) for attr in dir(self) if type(getattr(self, attr)) is User]
            for user in user_list:
                if not user:
                    continue
                user.checkCallbackEmpty()

            return ret

        wrapper.case_func_name = func.__name__  # 用例函数名
        wrapper.case_id = id  # 用例ID
        wrapper.case_cn_name = name  # 用例中文名
        wrapper.case_creator = creator  # 用例创建人
        wrapper.user_num = user_num  # 用户数，设备数
        wrapper.case_platform = os  # 用例所支持的平台
        return wrapper

    return decorator
